//
//  UIPopoverListView.m
//  UIPopoverListViewDemo
//
//  Created by su xinde on 13-3-13.
//  Copyright (c) 2013年 su xinde. All rights reserved.
//

#import "UIPopoverListView.h"
#import <QuartzCore/QuartzCore.h>

//#define FRAME_X_INSET 20.0f
//#define FRAME_Y_INSET 40.0f

@interface UIPopoverListView ()
@property (nonatomic,strong)UIButton *selectButton;
@property (nonatomic,strong)NSMutableArray *buttonArray;
- (void)defalutInit;
- (void)fadeIn;
- (void)fadeOut;

@end

@implementation UIPopoverListView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.buttonArray = [NSMutableArray array];
        [self defalutInit];
    }
    return self;
}

- (void)defalutInit
{
  
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5.0f;
    self.clipsToBounds = TRUE;
    CGFloat xWidth = self.bounds.size.width;
    
    _titleView = [UILabel new];
    [self addSubview:_titleView];
    _titleView.font = [UIFont systemFontOfSize:13.0f];//#e5e5e5
    _titleView.backgroundColor = [UIColor colorWithHexString:@"#03Ab9e"];
    
    _titleView.textAlignment = NSTextAlignmentCenter;
    _titleView.textColor = [UIColor whiteColor];
    [_titleView setText:@"您可以选择金额,也可以自定义输入金额"];
    [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.left.equalTo(self).with.offset(0);
        make.height.mas_equalTo(@35);
    }];


    NSArray *titleButtonArray = [NSArray arrayWithObjects:@"1元",@"5元",@"10元",@"50元",@"100元",@"500元",@"自定义", nil];
    CGFloat buttonW = 60;
    CGFloat buttonH = 28;
    CGFloat marge = (xWidth-3*60)/4;
    
    for (int i = 0 ; i < 7; i++) {
        NSInteger index = i % 3;
        NSInteger page = i / 3;
        // 圆角按钮
        self.button= [UIButton buttonWithType:UIButtonTypeCustom];
        
        self.button.frame = CGRectMake(index * (buttonW + marge) + marge, page  * (buttonH + 10)+50, buttonW, buttonH);
        self.button.tag = i;
        [self.button setTitle:titleButtonArray[i] forState:UIControlStateNormal];
        if (i==0) {
            [self.button setSelected:YES];
            self.selectButton = self.button;
        }
        [self.button setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor colorWithHexString:@"#03Ab9e"] forState:UIControlStateSelected];
        [self.button setBackgroundImage:[UIImage imageNamed:@"main_InvolvedDeatails_cell_n"] forState:UIControlStateNormal];
        [self.button setBackgroundImage:[UIImage imageNamed:@"main_InvolvedDeatails_cell_p"] forState:UIControlStateSelected];
        self.button.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.button addTarget:self action:@selector(moneyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.button];
        [self.buttonArray addObject:self.button];
      
    }
    UIButton *lastButton =(UIButton *)[self.buttonArray objectAtIndex:6];
    
    UIButton *okButton = [UIButton new];
    [self addSubview:okButton];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastButton.bottom).with.offset(25);
        make.right.equalTo(self).with.offset(-30);
        make.left.equalTo(self).with.offset(30);
        make.height.mas_equalTo(@30);
    }];
    okButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [okButton setTitle:@"确认" forState:UIControlStateNormal];
    okButton.layer.cornerRadius = 5.0f;
    [okButton addTarget:self action:@selector(ClickoKButton) forControlEvents:UIControlEventTouchUpInside];
    [okButton setBackgroundColor: [UIColor colorWithHexString:@"#03ab9e"]];

    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    [_overlayView addTarget:self
                     action:@selector(dismiss)
           forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)moneyButtonClick:(UIButton *)sender{

    if (self.selectButton != sender) {
        self.selectButton.selected = NO;
        self.selectButton = sender;
    }
    self.selectButton.selected = YES;
}

-(void)ClickoKButton{
    [self dismiss];
    if (self.selectButton) {
        self.moneyButtonClik(self.selectButton.tag,self.selectButton.titleLabel.text);
    }
}

#pragma mark - animations

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}

- (void)setTitle:(NSString *)title
{
    _titleView.text = title;
}

- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,
                              keywindow.bounds.size.height/2.0f);
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}

@end
